package com.taxsystem.gaozhuanglin.invoicebusiness;

import java.util.concurrent.ExecutionException;

import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.HttpUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	
	EditText kjxh;
	EditText kplxdm;
	EditText fpzldm;
	
	Button registerNext;
	Button registerClear;
	
	private LocalBroadcastManager localBroadcastManager2 = null;
	
	private String m1;
	private String m2;
	private String m3;
	private String m4;
	private String m5;
	
	private Handler handler = null;
	
	
	private final static String url = "";
	private final static String ACTION_REGISTER = "RegisterActivity.HTTP_ACTION";
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		
		kjxh = (EditText)findViewById(R.id.kjxh_edit);
		kplxdm = (EditText)findViewById(R.id.kplxdm_edit);
		fpzldm = (EditText)findViewById(R.id.fpzldm_edit);
		
		registerNext = (Button)findViewById(R.id.nextBtn_register);
		registerClear = (Button)findViewById(R.id.clearBtn_register);
		
		//注册Broadcast Receive
				/*localBroadcastManager2 = LocalBroadcastManager.getInstance(RegisterActivity.this);
				localBroadcastManager2.registerReceiver(new BroadcastReceiver(){
					@Override
					public void onReceive(Context context, Intent intent) {
						//获取JSON，并进行解析
					}
				}, new IntentFilter(ACTION_REGISTER));
				
				//new MyThread2().start();*/
		
		handler = new Handler()
		{
			public void handleMessage(Message msg)
			{
				super.handleMessage(msg);
				if(msg.what==0x345)
				{
					Intent intent = new Intent(RegisterActivity.this, RegisterInformationActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("kjxh", m1);
					bundle.putString("kplxDm", m2);
					bundle.putString("fpzlDm", m3);
					bundle.putString("djIp", m4);
					bundle.putString("mobile", m5);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			}
		};
				
				registerNext.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						/*if(kjxh.getText().toString().equals("") || kplxdm.getText().toString().equals("") || fpzldm.getText().toString().equals(""))
						{
							Toast toast = new Toast(RegisterActivity.this);
							toast.setGravity(Gravity.BOTTOM, 0, 0);
							toast.setText("请输入完整信息!");
							toast.setDuration(Toast.LENGTH_SHORT);
							toast.show();
						}
						else
						{
							toGetRequest();
						}*/
						Intent intent = new Intent(RegisterActivity.this, RegisterInformationActivity.class);
						startActivity(intent);
					}		
				});
				
				//清空内容
				registerClear.setOnClickListener(new OnClickListener()
				{

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						kjxh.setText("");
						kplxdm.setText("");
						fpzldm.setText("");
					}
					
				});
	}
	
	
	protected void toGetRequest()
	{
		new Thread(new Runnable()
		{

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message m = new Message();
				m.what = 0x345;
				try {
					String result = HttpUtil.getRequest(url);
					JSONObject json = new JSONObject(result);
					m1 = json.getString("kjxh");
					m2 = json.getString("kplxDm");
					m3 = json.getString("fpzlDm");
					m4 = json.getString("djIp");
					m5 = json.getString("mobile");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				handler.sendMessage(m);
			}
			
		}).start();
	}
	
	/*private class MyThread2 extends Thread{

		@Override
		public void run() {
			//TODO:add your code here
			Intent intent2 = new Intent(ACTION_REGISTER);
			try {
				String string1 = kjxh.getText().toString();
				String string2 = kplxdm.getText().toString();
				String string3 = fpzldm.getText().toString();
				
				String result = HttpUtil.getRequest(url);
				intent2.putExtra("invoice_information", result);
				localBroadcastManager2.sendBroadcast(intent2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
	}*/
}
